/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.core.mongo.connection.internal;

import com.mongodb.Mongo;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.Validate;
import org.jrose.api.annotation.persistence.MongoEntity;
import org.jrose.core.mongo.MongoConnection;
import org.jrose.core.mongo.connection.DatabaseNameDecorator;
import org.jrose.core.mongo.connection.MongoConnectionHolder;
import org.jrose.core.mongo.connection.MongoConnectionManager;
import org.jrose.core.util.AnnotationUtils;
import org.jrose.core.util.ApplicationContextProvider;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor
public class MongoConnectionManagerImpl extends ApplicationContextProvider implements InitializingBean, MongoConnectionManager, DisposableBean {
    @Setter private MongoConnection mongoConnection;
    @Setter private DatabaseNameDecorator databaseNameDecorator;

    private Mongo mongo;
    private final Map<String, MongoConnectionHolder> connectionCache = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        Validate.notNull(mongoConnection, "'mongoConnection' no defined");
        mongo = mongoConnection.getMongo();
    }

    @Override
    public void destroy() throws Exception {
        for (MongoConnectionHolder connection : connectionCache.values()) {
            connection.close();
        }
    }

    @Override
    public MongoConnectionHolder getMongoConnection(Class<?> entityClass) {
        Validate.notNull(entityClass, "'entityClass' must not be null");
        MongoEntity annotation = AnnotationUtils.getAnnotation(entityClass, MongoEntity.class);
        Validate.notNull(annotation, "No @MongoEntity found on '%s'", entityClass.getName());

        synchronized (connectionCache) {
            if (connectionCache.containsKey(entityClass.getName())) {
                return connectionCache.get(entityClass.getName());
            } else {
                MongoConnectionHolder connection = new MongoConnectionHolderImpl(mongo, annotation, databaseNameDecorator, applicationContext);
                connectionCache.put(entityClass.getName(), connection);
                return connection;
            }
        }
    }
}
